package vashion.azeroth.core.manager.personal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.common.encrypt.Md5Encrypt;
import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.core.common.NeedOperateFlow;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.personal.dao.PersonalMemberDao;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.pojo.common.OperateFlowEnum;
import vashion.azeroth.core.result.ApiResult;

@Service
public class PersonalMemberManager extends BaseManagerImpl<PersonalMember> {

	private final static Logger log = LoggerFactory.getLogger(PersonalMemberManager.class);

	@Autowired
	private PersonalMemberDao memberDao;

	public PersonalMember getMemberByName(String memberName) {
		return ((PersonalMemberDao) getDao()).getMemberByName(memberName);
	}

	public ArrayList<PersonalMember> findNoGroupAgent(Long memberId) {
		return ((PersonalMemberDao) getDao()).findNoGroupAgent(memberId);
	}

	public ArrayList<PersonalMember> queryGroupMember(Long groupId) {
		return ((PersonalMemberDao) getDao()).queryGroupMember(groupId);
	}

	public List<PersonalMember> find(PersonalMember member) {
		return getDao().find(member);
	}

	public int findCnt(PersonalMember member) {
		return getDao().findCnt(member);
	}

	@Override
	public BaseDao<PersonalMember> getDao() {
		return memberDao;
	}

	public String findOldPwd(Long memberId) {
		return memberDao.findOldPwd(memberId);
	}

	public boolean updatePwd(String newPwd) {
		return memberDao.updatePwd(newPwd);
	}

	public PersonalMember getMemberByNick(String nickName) {
		return memberDao.getMemberByNick(nickName);
	}

	public PersonalMember findHigherBymemberId(PersonalMember member) {
		return memberDao.findHigherBymemberId(member);
	}

	public ArrayList<PersonalMember> findLowererByInviteMemberId(PersonalMember member) {
		return memberDao.findLowererByInviteMemberId(member);
	}

	public List<PersonalMember> getAdminList() {
		PersonalMember personalMember = new PersonalMember();
		personalMember.setType(AzerothConstants.PersonalMember.MemberType.supplier);
		return memberDao.find(personalMember);
	}

	/**
	 * 生成供应商账号
	 * 
	 * @param memberParam
	 * @param inviteCode
	 * @return
	 */
	@NeedOperateFlow(operateIdArgLocation = 1, operateFlowEnum = OperateFlowEnum.MEMBER)
	public PersonalMember generateProviderAccount(PersonalMember memberParam, long operateId) {
		memberParam.setStatus(AzerothConstants.PersonalMember.PersonalMemberState.enable);
		memberParam.setTimeAdd(new Date());
		memberParam.setTimeEdit(new Date());
		memberParam.setMemberPwd(Md5Encrypt.encrypt(memberParam.getMemberPwd(), "UTF-8"));
		memberParam.setType(AzerothConstants.PersonalMember.MemberType.supplier);
		return this.save(memberParam);
	}

	/**
	 * 冻结用户账号
	 * 
	 * @param memberId
	 * @param operateId
	 * @return 冻结的结果
	 */
	@NeedOperateFlow(operateIdArgLocation = 1, operateFlowEnum = OperateFlowEnum.MEMBER)
	public ApiResult<Boolean> freezeMember(long memberId, long operateId) {
		PersonalMember member = this.get(memberId);
		if (null == member) {
			return ApiResult.newErrorResult(MemberErrorEnum.PROIFLES_NODATA.getErrorCode(),
					MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
		}
		if (AzerothConstants.PersonalMember.MemberType.supplier != member.getType()
				&& AzerothConstants.PersonalMember.MemberType.agent != member.getType()) {
			return ApiResult.newErrorResult(MemberErrorEnum.PROIFLES_NODATA.getErrorCode(),
					MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
		}
		boolean updateSuccess;
		try {
			member = new PersonalMember();
			member.setMemberId(memberId);
			member.setStatus(AzerothConstants.PersonalMember.PersonalMemberState.disabled);
			updateSuccess = this.update(member) > 0;
		} catch (Exception e) {
			log.error("PersonalMemberManager.freezeMember error, operateId=" + operateId, e);
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return updateSuccess ? ApiResult.newSuccessResult(true)
				: ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
						GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(), false);
	}

	/**
	 * 解冻用户账号
	 * 
	 * @param memberId
	 * @param operateId
	 * @return 解冻的结果
	 */
	@NeedOperateFlow(operateIdArgLocation = 1, operateFlowEnum = OperateFlowEnum.MEMBER)
	public ApiResult<Boolean> resumeMember(long memberId, long operateId) {
		PersonalMember member = this.get(memberId);
		if (null == member) {
			return ApiResult.newErrorResult(MemberErrorEnum.PROIFLES_NODATA.getErrorCode(),
					MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
		}
		if (AzerothConstants.PersonalMember.MemberType.supplier != member.getType()
				&& AzerothConstants.PersonalMember.MemberType.agent != member.getType()) {
			return ApiResult.newErrorResult(MemberErrorEnum.PROIFLES_NODATA.getErrorCode(),
					MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
		}
		boolean updateSuccess;
		try {
			member = new PersonalMember();
			member.setMemberId(memberId);
			member.setStatus(AzerothConstants.PersonalMember.PersonalMemberState.enable);
			updateSuccess = this.update(member) > 0;
		} catch (Exception e) {
			log.error("PersonalMemberManager.freezeMember error, operateId=" + operateId, e);
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
		return updateSuccess ? ApiResult.newSuccessResult(true)
				: ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
						GenericErrorEnum.SYSTEM_ERROR.getErrorMessage(), false);
	}

	/**
	 * 保存用于推送通知用的手机设备的RegistrationId
	 * 
	 * @param member
	 * @return 保存结果
	 */
	public int updateRegistrationId(PersonalMember member) {
		return ((PersonalMemberDao) getDao()).updateRegistrationId(member);
	}
}
